package com.cskaoyan.service.Impls;


import com.cskaoyan.bean.vo.GoodsCreateVo;
import com.cskaoyan.mapper.MarketGoodsAttributeMapper;
import com.cskaoyan.mapper.MarketGoodsMapper;
import com.cskaoyan.mapper.MarketGoodsProductMapper;
import com.cskaoyan.mapper.MarketGoodsSpecificationMapper;
import com.cskaoyan.model.MarketGoods;
import com.cskaoyan.model.MarketGoodsAttribute;
import com.cskaoyan.model.MarketGoodsProduct;
import com.cskaoyan.model.MarketGoodsSpecification;
import com.cskaoyan.service.GoodsService;
import com.cskaoyan.util.MyBatisUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.Date;
import java.util.List;

public class GoodsServiceImpl implements GoodsService {
    @Override
    public void createGoods(GoodsCreateVo goodsCreateVo) {
        // 把这些操作放在一个事务中 → 使用同一个SqlSession
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        // 提供一个添加时间
        Date current = new Date();

        // MarketGoods goods → 向goods表中添加数据
        MarketGoodsMapper goodsMapper = sqlSession.getMapper(MarketGoodsMapper.class);
        MarketGoods goods = goodsCreateVo.getGoods();
        goods.setAddTime(current);
        goods.setUpdateTime(current);
        goodsMapper.insertSelective(goods); //生成自增的主键，作为接下来插入的表中的goods_id字段值
        //数据插入MarketGoodsMapper表
        Integer goodsId = goods.getId();

        // List<MarketGoodsSpecification> specifications → 向specification表中添加数据
        MarketGoodsSpecificationMapper specificationMapper = sqlSession.getMapper(MarketGoodsSpecificationMapper.class);
        List<MarketGoodsSpecification> specifications = goodsCreateVo.getSpecifications();
        for (MarketGoodsSpecification specification : specifications) {
            specification.setGoodsId(goodsId);
            specification.setAddTime(current);
            specification.setUpdateTime(current);
            specificationMapper.insertSelective(specification);//数据插入MarketGoodsSpecificationMapper表
        }

        // List<MarketGoodsProduct> products → 向product表中添加数据
        MarketGoodsProductMapper productMapper = sqlSession.getMapper(MarketGoodsProductMapper.class);
        List<MarketGoodsProduct> products = goodsCreateVo.getProducts();
        for (MarketGoodsProduct product : products) {
            product.setGoodsId(goodsId);
            product.setAddTime(current);
            product.setUpdateTime(current);
            productMapper.insertSelective(product);//数据插入MarketGoodsProduct表
        }

        // List<MarketGoodsAttribute> attributes → 向attribute表中添加数据
        MarketGoodsAttributeMapper attributeMapper = sqlSession.getMapper(MarketGoodsAttributeMapper.class);
        List<MarketGoodsAttribute> attributes = goodsCreateVo.getAttributes();
        for (MarketGoodsAttribute attribute : attributes) {
            attribute.setGoodsId(goodsId);
            attribute.setAddTime(current);
            attribute.setUpdateTime(current);
            attributeMapper.insertSelective(attribute);//数据插入MarketGoodsAttributeMapper表
        }

        // 一起提交事务
        sqlSession.commit();
        sqlSession.close();
    }
}
